package com.blacktech.dbu.auth.repository;

import com.blacktech.dbu.auth.entity.DbuOperationLog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 操作日志数据访问层
 *
 * @author Yanyunsong
 */
@Repository
public interface OperationLogRepository extends JpaRepository<DbuOperationLog, Long> {

    /**
     * 根据用户ID查找操作日志
     */
    Page<DbuOperationLog> findByUserId(Long userId, Pageable pageable);

    /**
     * 根据操作类型查找日志
     */
    Page<DbuOperationLog> findByOperationType(String operationType, Pageable pageable);

    /**
     * 根据时间范围查找日志
     */
    @Query("SELECT ol FROM DbuOperationLog ol WHERE ol.createTime BETWEEN :startTime AND :endTime")
    Page<DbuOperationLog> findByTimeRange(@Param("startTime") LocalDateTime startTime,
                                         @Param("endTime") LocalDateTime endTime,
                                         Pageable pageable);

    /**
     * 多条件查询操作日志
     */
    @Query("SELECT ol FROM DbuOperationLog ol WHERE " +
           "(:userId IS NULL OR ol.user.id = :userId) AND " +
           "(:operationType IS NULL OR ol.operationType LIKE %:operationType%) AND " +
           "(:objectType IS NULL OR ol.objectType = :objectType) AND " +
           "(:startTime IS NULL OR ol.createTime >= :startTime) AND " +
           "(:endTime IS NULL OR ol.createTime <= :endTime)")
    Page<DbuOperationLog> findByConditions(@Param("userId") Long userId,
                                          @Param("operationType") String operationType,
                                          @Param("objectType") String objectType,
                                          @Param("startTime") LocalDateTime startTime,
                                          @Param("endTime") LocalDateTime endTime,
                                          Pageable pageable);

    /**
     * 查找用户的登录日志
     */
    @Query("SELECT ol FROM DbuOperationLog ol WHERE ol.user.id = :userId AND ol.operationType IN ('LOGIN', 'LOGOUT', 'LOGIN_FAILED') ORDER BY ol.createTime DESC")
    List<DbuOperationLog> findUserLoginLogs(@Param("userId") Long userId);

    /**
     * 查找用户在指定时间范围内的操作日志
     */
    @Query("SELECT ol FROM DbuOperationLog ol WHERE ol.user.id = :userId AND ol.createTime BETWEEN :startTime AND :endTime ORDER BY ol.createTime DESC")
    List<DbuOperationLog> findUserLogsByTimeRange(@Param("userId") Long userId,
                                                  @Param("startTime") LocalDateTime startTime,
                                                  @Param("endTime") LocalDateTime endTime);

    /**
     * 统计各类操作的数量
     */
    @Query("SELECT ol.operationType, COUNT(ol) FROM DbuOperationLog ol WHERE ol.createTime BETWEEN :startTime AND :endTime GROUP BY ol.operationType")
    List<Object[]> countOperationsByType(@Param("startTime") LocalDateTime startTime,
                                         @Param("endTime") LocalDateTime endTime);

    /**
     * 删除指定日期之前的日志
     */
    @Query("DELETE FROM DbuOperationLog ol WHERE ol.createTime < :cutoffDate")
    void deleteLogsBefore(@Param("cutoffDate") LocalDateTime cutoffDate);
}